package com.xiangxiangjd.analyse.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiangxiangjd.analyse.controller.helper.$;
import com.xiangxiangjd.analyse.bean.RepairStructure;
import com.xiangxiangjd.analyse.mapper.RepairStructureMapper;
import com.xiangxiangjd.analyse.service.RepairStructureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangxiangjd.analyse.bean.result.ResultPage;
import com.xiangxiangjd.analyse.controller.model.ApiResultModel;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 肖守凯
 * @since 2021-03-08
 */
@Service
public class RepairStructureServiceImpl extends ServiceImpl<RepairStructureMapper, RepairStructure> implements RepairStructureService {
    /**
     * 添加RepairStructure
     *
     * @param repairStructure 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public RepairStructure addRepairStructure(RepairStructure repairStructure) {


        /**
         * 判断是否有相同的名字
         */
        RepairStructure repairStructures = baseMapper.selectOne(new LambdaQueryWrapper<RepairStructure>().eq(RepairStructure::getStructureName, repairStructure.getStructureName()));

        if (repairStructures!=null) {
          throw  new   RuntimeException(repairStructure.getStructureName()+":已有该名称,请重新填写名称!");
        } ;



        if (this.save(repairStructure)) {
            return repairStructure;
        } else {
            throw new   RuntimeException(repairStructure.getStructureName()+":添加失败 请重新添加");
        }
    }

    /**
     * 编辑跳转页面的回显数据RepairStructure
     *
     * @param id 前端传进的参数
     * @return 返回实体类
     */
    @Override
    public RepairStructure selectRepairStructureByIdRepairStructure(String id) {
        return this.getById(id);
    }


    /**
     * 编辑RepairStructure
     *
     * @param repairStructure 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel editRepairStructure(RepairStructure repairStructure) {
        if (this.updateById(repairStructure)) {
            return $.success();
        } else {
            return $.error("编辑失败");
        }
    }

    /**
     * 删除一条RepairStructure
     *
     * @param ids 前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel delRepairStructures(List ids) {

        int i = baseMapper.deleteBatchIds(ids);

        return $.success(i);

    }

    /**
     * 分页查询RepairStructure
     *
     * @param page            页码
     * @param limit           查几条
     * @param repairStructure 前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findRepairStructureByPage(Integer page, Integer limit, RepairStructure repairStructure) {
        Map
                <String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("repairStructure", repairStructure);
        List<RepairStructure> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<RepairStructure> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    /**
     * 获取树形结构
     *
     * @return
     */
    @Override
    public List<RepairStructure> getTreeStructure() {


        //查询所有的对象
        List<RepairStructure> repairStructuresList = baseMapper.selectList(null);

        //首先找出顶级的目录
        List<RepairStructure> collect = repairStructuresList.stream()
                //过滤 父级 id 等于0的 为顶级目录
                .filter(repairStructure -> repairStructure.getParentId().equals("0"))
                // 修改集合 为父级的自己目录 赋值
                .map(obj -> {
                    // 给子集赋值  传入当前的对象 和 全部的对象
                    obj.setChildren(getChildrens(obj, repairStructuresList));
                    return obj;
                })
                .collect(Collectors.toList());


        return collect;
    }

    /**
     * 递归 完成树形结构
     *
     * @param root 当前对象
     * @param all 所有对象
     * @return
     */
    private List<RepairStructure> getChildrens(RepairStructure root, List<RepairStructure> all) {
       return all.stream().filter(obj -> obj.getParentId().equals(root.getStructureId())).map((repairStructure) -> {
            repairStructure.setChildren(getChildrens(repairStructure, all));
            return repairStructure;
        }).collect(Collectors.toList());

    }
}
